package com.changgou.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ProjectName: changgou_parent
 * @Package: com.itheima.canal.config
 * @ClassName: RabbitMQConfig
 * @Author: 孙明田
 * @Description: rabbitmq配置类
 * @Date: 2020/6/17 22:38
 * @Version: 1.0
 */
@Configuration
public class RabbitMQConfig {
    //定义队列名
    public static final String AD_UPDATE_QUEUE = "ad_update_queue";
    public static final String GOODS_UP_QUEUE = "goods_up_queue";
    public static final String GOODS_DOWN_QUEUE = "goods_down_queue";

    //定义交换机名
    public static final String GOODS_UP_EXCHANGE = "goods_up_exchange";
    public static final String GOODS_DOWN_EXCHANGE = "goods_down_exchange";

    //声明队列
    @Bean("queue")
    public Queue queue() {
        return new Queue(AD_UPDATE_QUEUE);
    }

    @Bean(GOODS_UP_QUEUE)
    public Queue GOODS_UP_QUEUE() {
        return new Queue(GOODS_UP_QUEUE);
    }

    @Bean(GOODS_DOWN_QUEUE)
    public Queue GOODS_DOWN_QUEUE() {
        return new Queue(GOODS_DOWN_QUEUE);
    }

    //声明交换机
    @Bean(GOODS_UP_EXCHANGE)
    public Exchange GOODS_UP_EXCHANGE() {
        return ExchangeBuilder.fanoutExchange(GOODS_UP_EXCHANGE).durable(true).build();
    }

    @Bean(GOODS_DOWN_EXCHANGE)
    public Exchange GOODS_DOWN_EXCHANGE() {
        return ExchangeBuilder.fanoutExchange(GOODS_DOWN_EXCHANGE).durable(true).build();
    }

    //绑定交换机与队列
    @Bean
    public Binding GOODS_UP_EXCHANGE_QUEUE(@Qualifier(GOODS_UP_EXCHANGE) Exchange exchange, @Qualifier(GOODS_UP_QUEUE) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    @Bean
    public Binding GOODS_DOWN_EXCHANGE_QUEUE(@Qualifier(GOODS_DOWN_EXCHANGE) Exchange exchange, @Qualifier(GOODS_DOWN_QUEUE) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
}
